// const http = 'http://47.245.100.158/front_end'
const http = 'https://school.duoyue.net/api/v1'


export default function Request(url, data = {}, method, header = {}, timeout = 60000) {
	uni.showLoading({
		title: "加载中..."
	})

	let body = data.params ? {} : data

	let queryString = data.params ? Object.keys(data.params)
		.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data.params[key])}`)
		.join('&') : "";
	console.log(queryString, data, "get请求单数")
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${url.indexOf('http')!=-1?'':http}${url}${queryString?'?'+queryString:""}`,
			method: method,
			data: body,
			header: {
				...header
			},
			timeout: timeout,
			success: (res) => {
				// 将结果抛出
				// resolve(res.data)
			},
			fail: (err) => {
				// resolve(err)
			},
			complete: (response) => {
				uni.hideLoading()
				console.log(response, "响应拦截")
				if (response.statusCode === 200) {
					switch (response.data.errcode) {
						case 0:
							resolve(response.data.content)
							break;
						case 4004:
							// 错误码404的处理方式
							console.error("登录失效")
							uni.showToast({
								icon: "none",
								title: response.data.msg
							})
							uni.removeStorageSync('userinfo')
							uni.removeStorageSync('token')
							uni.reLaunch({
								url: "/pages/login"
							})
							break;
						default:
							uni.showToast({
								icon: "none",
								title: response.data.content.data || response.data.errmsg
							})
							reject(response)
							break;
					}
				} else {
					reject(response)
				}
			}
		})
	})
}